package com.LGF.Book.biz;

import com.LGF.Book.bean.Book;
import com.LGF.Book.bean.BookType;
import com.LGF.Book.dao.BookDao;
import com.LGF.Book.dao.BookTypeDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BookBiz {
    //创建BookDao对象
    BookDao bookDao = new BookDao();
    BookTypeDao bookTypeDao = new BookTypeDao();

    /**
     * 调用dao层根据id获取图书信息
     * @param id
     * @return
     */
    public Book getById(long id){
        Book book = null;
        try {
            book = bookDao.getById(id);
            //根据类型名获取对象
            BookType bookType = bookTypeDao.getById(book.getId());
            book.setType(bookType);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    /**
     * 调用dao层获取所有图书信息
     * @return
     */
    public List<Book> getAll(){
        List<Book> books = null;
        try {
            books = bookDao.getAll();
            //根据类型获取对象信息
            for (Book book: books) {
                BookType bookType = bookTypeDao.getById(book.getTypeId());
                book.setType(bookType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    /**
     * 调用dao层方法分页查询图书信息
     * @param page
     * @param limit
     * @return
     */
    public List<Book> getPage(int page,int limit){
        List<Book> books = null;
        try {
            books = bookDao.getPage(page, limit);


            //根据类型获取对象信息
            for (Book book: books) {
                BookType bookType = bookTypeDao.getById(book.getTypeId());
                book.setType(bookType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    /**
     * 调用dao层添加图书信息
     * @param typeId
     * @param bname
     * @param price
     * @param desc
     * @param pic
     * @param publish
     * @param author
     * @param stock
     * @param address
     * @return
     */
    public int add(long typeId,String bname,float price, String desc,String pic,String publish,
                   String author,long stock,String address)  {

        int count = 0;
        try {
            count = bookDao.add(typeId, bname, price, desc, pic, publish, author, stock, address);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    //如果接收的是对象可以用对象.属性，调用
    public  int add(Book book){
        return add(book.getTypeId(),book.getBname(),book.getPrice(),book.getDesc(),book.getPic(),book.getPublish(),book.getAuthor(),book.getStock(),book.getAddress());

    }



    /**
     * 调用dao层修改图书信息
     * @param id
     * @param typeId
     * @param bname
     * @param price
     * @param desc
     * @param pic
     * @param publish
     * @param author
     * @param stock
     * @param address
     * @return
     */
    public int modify(long id, long typeId,String bname,float price, String desc,String pic,String publish,
                   String author,long stock,String address)  {
        int count = 0;
        try {
            count = bookDao.modify(id, typeId, bname, price, desc, pic, publish, author, stock, address);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public int modify(Book book)  {
        int count = 0;
        try {
            count = bookDao.modify(book.getId(),book.getTypeId(),book.getBname(), book.getPrice(), book.getDesc(), book.getPic(), book.getPublish(), book.getAuthor(), book.getStock(), book.getAddress());
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return count;
    }

    /**
     * 调用dao层根据id删除图书信息
     * @param id
     * @return
     */
    public int remove(long id){
        int count = 0;
        try {
            count = bookDao.remove(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 调用dao层根据书名模糊查询图书信息
     * @param bname
     * @return
     */
    public List<Book> selectByBname(String bname){
        List<Book> books = null;
        try {
            books = bookDao.selectByBname(bname);
            //根据类型获取对象信息
            for (Book book: books) {
                BookType bookType = bookTypeDao.getById(book.getTypeId());
                book.setType(bookType);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    /**
     * 调用dao层根据id模糊查询图书信息
     * @param id
     * @return
     */
    public List<Book> selectById(String id){
        List<Book> books = null;
        try {
            books = bookDao.selectById(id);
            //根据类型获取对象信息
            for (Book book: books) {
                BookType bookType = bookTypeDao.getById(book.getTypeId());
                book.setType(bookType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }

    /**
     * 根据图书类型模糊查询图书信息
     * @param
     * @return
     */
    public List<Book> selectByType(String tname){
        //创建List<Book>数组
        List<Book> books = new ArrayList<>();
        //创建bookTypebiz对象
        BookTypeBiz bookTypeBiz = new BookTypeBiz();
        BookBiz bookBiz = new BookBiz();
        //模糊查询图书类型
        List<BookType> bookTypes = bookTypeBiz.selectTName(tname);
        System.out.println(bookTypes);

        //获取所有图书信息
        List<Book> all = bookBiz.getAll();
        for (Book book: all) {
            for (BookType bookType : bookTypes) {
                    if (book.getTypeId()==bookType.getId()){
                        books.add(book);
                    }
            }
        }




//            try {
//                Book book = bookBiz.getById(bookType.getId());
//                System.out.println(book);
//                book.setType(bookType);
//                    books.add(book);
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }

        return books;
    }

    /**
     * 调用dao层根据图书类型模糊查询图书信息
     * @param tname
     * @return
     */
    public List<Map<String,Object>> selectTname(String tname){
        List<Map<String, Object>> data = null;
        try {
            data = bookDao.selectTname(tname);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return data;

    }

    /**
     * 根据类型id查询图书信息
     * @param typeId
     * @return
     */
    public Book getBytypeId(long typeId){
        Book book = null;
        try {
            book = bookDao.getBytypeId(typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    public static void main(String[] args) {
        BookBiz bookBiz = new BookBiz();
//        List<Book> page = bookBiz.getPage(0, 3);
//        System.out.println(page.size());
//        System.out.println(page.toString());
        List<Book> books = bookBiz.selectByType("学");
        System.out.println(books.size());
        System.out.println(books.toString());
//        List<Map<String, Object>> m = bookBiz.selectTname("学");
//        System.out.println(m.toString());
//        System.out.println(m.size());
    }


    }
